package net.maku.association.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.association.convert.InterestAssociationDynamicsConvert;
import net.maku.association.entity.InterestAssociationDynamicsEntity;
import net.maku.association.service.InterestAssociationDynamicsService;
import net.maku.association.query.InterestAssociationDynamicsQuery;
import net.maku.association.vo.InterestAssociationDynamicsVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 协会动态
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2025-04-26
*/
@RestController
@RequestMapping("association/dynamics")
@Tag(name="协会动态")
@AllArgsConstructor
public class InterestAssociationDynamicsController {
    private final InterestAssociationDynamicsService interestAssociationDynamicsService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('association:interest_association_dynamics:page')")
    public Result<PageResult<InterestAssociationDynamicsVO>> page(@ParameterObject @Valid InterestAssociationDynamicsQuery query){
        PageResult<InterestAssociationDynamicsVO> page = interestAssociationDynamicsService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('association:interest_association_dynamics:info')")
    public Result<InterestAssociationDynamicsVO> get(@PathVariable("id") Long id){
        InterestAssociationDynamicsEntity entity = interestAssociationDynamicsService.getById(id);

        return Result.ok(InterestAssociationDynamicsConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('association:interest_association_dynamics:save')")
    public Result<String> save(@RequestBody InterestAssociationDynamicsVO vo){
        interestAssociationDynamicsService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('association:interest_association_dynamics:update')")
    public Result<String> update(@RequestBody @Valid InterestAssociationDynamicsVO vo){
        interestAssociationDynamicsService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('association:interest_association_dynamics:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        interestAssociationDynamicsService.delete(idList);

        return Result.ok();
    }
}